﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmKhuyettat : Form
    {
        public frmKhuyettat()
        {
            InitializeComponent();
        }
        cls_DM_Khuyettat objKhuyettat = new cls_DM_Khuyettat();
        private void frmKhuyettat_Load(object sender, EventArgs e)
        {
            GetAll_Khuyettat();
        }
        private void GetAll_Khuyettat()
        {
            gridControl1.DataSource = objKhuyettat.GetAll_Khuyettat();
        }
        private void Save_Khuyettat()
        {
            clsDM_khuyettat objdmkt = new clsDM_khuyettat();
            objdmkt.TenKhuyetTat = gridView1.GetFocusedRowCellValue("TenKhuyetTat").ToString();
            objdmkt.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmkt.KhuyetTatID = 1;
                objdmkt.isChangeable = false;
                blnResult = objKhuyettat.Insert(objdmkt);
            }
            else
            {
                objdmkt.KhuyetTatID = (int)gridView1.GetFocusedRowCellValue("KhuyetTatID");
                objdmkt.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objKhuyettat.update(objdmkt);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Khuyettat();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenKhuyetTat").ToString()) == DialogResult.No)
                return;
            objKhuyettat.delete((int)gridView1.GetFocusedRowCellValue("KhuyetTatID"));
            GetAll_Khuyettat();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Khuyettat();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }
    }
}
